<?php
namespace app\api\controller;
// use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
use think\captcha\Captcha;
use \app\api\model\Users as m;
class Index extends Common
{
    public $obj;
	public $obj1;
	function __construct(){
		parent::__construct();

	}
// 前端接口-------开始
    // 首页轮播图
    public function banner(){
        // Where条件
        $w['pid'] = array('eq',1);
        $w['ty'] = array('eq',4);
        
        $lists = lists('news',$w,'id,title,img1,linkurl');
        if($lists){
            $res['meta'] = array('msg'=>'获取栏目列表成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取栏目列表失败','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

    // 首页推荐
    public function goods_list(){
        $db=Db::name("products");
        // Where条件
        $w['pid'] = array('eq',2);
        $w['isgood'] = array('eq',1);
        if($query)$w['title'] = array('like','%'.$query.'%');
        
        $lists = $db->field('id,title,img1,price,price2')->where($w)
        ->order('disorder desc,sendtime desc,id desc')
        ->select();
        if($lists){
            $res['meta'] = array('msg'=>'获取推荐商品成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取推荐商品失败','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

    // 产品列表分页
    public function shop_list(){
        $db=Db::name("products");
        $query = input('get.query');//关键字
        $pagenum = input('get.pagenum');//当前页码
        $pagesize = input('get.pagesize');//当前显示行数

        // Where条件
        $w['pid'] = array('eq',2);
        if($query)$w['title'] = array('like','%'.$query.'%');
        
        $lists = $db->field('id,title,img1,price,price2')->where($w)
        ->order('disorder desc,sendtime desc,id desc')
        ->limit(($pagenum - 1) * $pagesize,$pagesize)
        // ->fetchSql(true)
        ->select();

        $count = $db->field('id,title,img1,price,price2')->where($w)->count();
        $res['total'] = round($count/$pagesize);
        // return dump($lists);
        if($lists){
            $res['meta'] = array('msg'=>'获取商品列表成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取商品列表失败','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

    // 产品详情
    public function pro_detail($value='')
    {
        // 1Where条件
        $id = input('id',0);
        $w['id'] = array('eq',$id);
        $lists = get_finds('products',$w,'id,title,price,price2,num,stock,hits','disorder asc,id asc');
        $res['message'] = $lists;
        // 2详情轮播
        $arr = Db::name('products')->find($id);
        $xw['pid'] = array('eq',$arr['id']);
        $xw['ty'] = array('eq',$arr['ty']);
        $xw['tty'] = array('eq',$arr['tty']);
        $xw['ttty'] = array('eq',$arr['ttty']);
        $xw['type'] = array('eq',0);
        $banner = lists('titlepic',$xw,'id,img1','disorder asc,id asc');
        $res['banner'] = $banner;
        // 3图文详情
        $xw['type'] = array('eq',1);
        $messimage = lists('titlepic',$xw,'id,img1','disorder asc,id asc');
        $res['messimage'] = $messimage;

        if($lists){
            $res['meta'] = array('msg'=>'获取详情信息成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取详情信息失败','status'=>201);
        }
        return json_encode($res);
    }

    // 资讯列表分页
    public function news(){
        $db=Db::name("news");
        $query = input('get.query');//关键字
        $pagenum = input('get.pagenum');//当前页码
        $pagesize = input('get.pagesize');//当前显示行数

        // Where条件
        $w['pid'] = array('eq',9);
        if($query)$w['title'] = array('like','%'.$query.'%');
        
        $lists = $db->field('id,title,img1,introduce,from_unixtime(sendtime,"%Y-%m-%d") as sendtime,hits')->where($w)
        ->order('disorder desc,sendtime desc,id desc')
        ->limit(($pagenum - 1) * $pagesize,$pagesize)
        // ->fetchSql(true)
        ->select();
        foreach ($lists as $k => $v) {
            if($v['img1']){
                // 检测图片地址
                $lists[$k]['img1'] = strpos($v['img1'],'http')===false?$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$v['img1']:$v['img1'];
            }
        }
        $count = $db->where($w)->count();
        $res['total'] = round($count/$pagesize);
        // return dump($lists);
        if($lists){
            $res['meta'] = array('msg'=>'获取商品列表成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取商品列表失败','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

    // 联系我们
    public function contact(){
        // Where条件
        $w['pid'] = array('eq',13);
        $w['ty'] = array('eq',14);
        
        $lists = lists('news',$w,'id,title,img1,introduce');
        if($lists){
            $res['meta'] = array('msg'=>'获取数据成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取数据失败','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

    // 联系我们-地图
    public function map(){
        $str=Db::name("config")->where("varname='sys_ll'")->find();
        if($str['varvalue']){
            $ditu = explode(',', $str['varvalue']);
            $res['meta'] = array('msg'=>'获取数据成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取数据失败','status'=>201);
        }

        $res['message'] = $ditu;
        return json_encode($res);
    }

    // 社区图片-左栏目
    public function communityleft(){
        // Where条件
        $w['pid'] = array('eq',15);
        
        $lists = lists('sort',$w,'id,catname','disorder asc,id asc');
        if($lists){
            $res['meta'] = array('msg'=>'获取数据成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'获取数据失败','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

    // 社区图片-右内容
    public function communityright(){
        // Where条件
        $w['pid'] = array('eq',15);
        $w['ty'] = array('eq',input('id'));

        $lists = lists('news',$w,'id,title,img1','disorder desc,id desc');
        if($lists){
            $res['meta'] = array('msg'=>'获取数据成功','status'=>200);
        }else{
            $res['meta'] = array('msg'=>'没有数据','status'=>201);
        }

        $res['message'] = $lists;
        return json_encode($res);
    }

// 前端接口-------结束
public function Index(){
    $info = array(
        '服务器名'=>$_SERVER["SERVER_NAME"],
        '服务器IP'=>gethostbyname($_SERVER['SERVER_NAME']),
        '服务器端口'=>$_SERVER["SERVER_PORT"],
        '网站文档目录'=>$_SERVER["DOCUMENT_ROOT"],
        '浏览器信息'=>substr($_SERVER['HTTP_USER_AGENT'], 0, 40),
        '通信协议'=>$_SERVER['SERVER_PROTOCOL'],
        '请求方法'=>$_SERVER['REQUEST_METHOD'],

        '服务器时间'=>date("Y年m月d日H点i分s秒"),
        'PHP版本'=>PHP_VERSION,
        '运行环境'=>$_SERVER["SERVER_SOFTWARE"],
        'PHP运行方式'=>php_sapi_name(),
        '操作系统'=>PHP_OS,
        '脚本超时时间'=>get_cfg_var("max_execution_time").'秒',
        '站点物理路径'=>realpath("./"),
        '脚本上传文件大小限制'=>get_cfg_var("upload_max_filesize")?get_cfg_var("upload_max_filesize"):"不允许上传附件",
        'POST提交内容限制'=>get_cfg_var("post_max_size"),
        '服务器语种'=>getenv("HTTP_ACCEPT_LANGUAGE"),
        '脚本运行时可占最大内存'=>get_cfg_var("memory_limit")?get_cfg_var("memory_limit"):"无",
        '剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M',
        'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF",
        'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO',
        'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO',
    );

    $this->assign('list',$info);
    return $this->fetch("/index/index");
}


    

    public function Logout(){
        session('admin_id',null);
        session('admin_uid',null);
        session('admin_username',null);
        session('admin_role_auth_ids',null);
        session('admin_userpwd',null);
        session('admin_role_name',null);
        $this->success("退出成功",url("login/Index"));
        exit();
     }

}